package com.cuckoo.mall.thus.order.orderlist;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bgn.baseframe.base.BaseActivity;
import com.cuckoo.mall.R;
import com.cuckoo.mall.thus.order.orderlist.paidorder.PaidOrderFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：wl on 2018/4/ * 邮箱：wanglun@stosz.com
 */
@Route(path = "/mall/orderlistActivity", group = "mall")
public class OrderlistActivity extends BaseActivity {

    @BindView(R.id.rb_left)
    RadioButton rbLeft;
    @BindView(R.id.rb_right)
    RadioButton rbRight;
    @BindView(R.id.rg_tab)
    RadioGroup rgTab;
    @BindView(R.id.vp_order)
    ViewPager vpOrder;
    MyViewPagerAdapter myViewPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        getTitleBar().setTitle(getString(R.string.order_list_titile));
//                .setRightIcon(R.mipmap.service_ico).setRightTextClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                ToastUtil.showInfo("service not open yet");
//            }
//        });
        initView();
    }

    private void initView() {
        myViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), getFragments());
        vpOrder.setAdapter(myViewPagerAdapter);
        vpOrder.setOnPageChangeListener(new MyPageChangeListener());
        vpOrder.setOffscreenPageLimit(2);
        rbLeft.setChecked(true);
        //vpOrder.setCurrentItem(getIntent().getIntExtra("tag", 0));
    }

    private List<Fragment> getFragments() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new PaidOrderFragment());
        // fragments.add(new UnpayorderFragment());

        return fragments;
    }

    @OnClick({R.id.rb_left, R.id.rb_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rb_left:
                rbLeft.setChecked(true);
                vpOrder.setCurrentItem(0);
                break;
            case R.id.rb_right:
                rbRight.setChecked(true);
                vpOrder.setCurrentItem(1);
                break;
        }
    }

    class MyViewPagerAdapter extends FragmentPagerAdapter {

        private List<Fragment> mFragments;

        /**
         * @param fm
         */
        public MyViewPagerAdapter(FragmentManager fm) {
            super(fm);

        }

        public MyViewPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            mFragments = fragments;
        }

        @Override
        public Fragment getItem(int arg0) {
            return mFragments.get(arg0);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

    }

    class MyPageChangeListener implements ViewPager.OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }

        @Override
        public void onPageSelected(int position) {
            switch (position) {
                case 0:
                    rbLeft.setChecked(true);

                    break;
                case 1:
                    rbRight.setChecked(true);

                    break;
            }
        }

    }

}
